package com.example.leetdemo.process;


/**
 * 博弈论问题
 */
public class Solution2029 {

    public boolean stoneGameIX(int[] stones) {
        int[] count = new int[3];
        for (int i = 0; i < stones.length; i++) {
            count[stones[i] % 3]++;
        }
        Integer alice = 0;
        while (count[0] + count[1] + count[2] > 0) {
            alice = giveStone(alice, count);
            if (alice == -1) {
                return false;
            }
            if (count[0] + count[1] + count[2] > 0) {
                alice = giveStone(alice, count);
                if (alice == -1) {
                    return true;
                }
            }
        }
        return false;
    }

    public int giveStone(Integer stone, int[] count) {
        if (stone == 0) {
            if (count[1] == count[2] && count[1] == 0) {
                return -1;
            }
            if (count[1] <= count[2] && count[1] > 0) {
                stone += 1;
                count[1]--;
            } else {
                stone += 2;
                count[2]--;
            }
        } else if (stone == 1) {
            if (count[1] == count[0] && count[0] == 0) {
                return -1;
            }
            if (count[0] <= count[1] && count[0] > 0) {
                count[0]--;
            } else {
                stone += 1;
                count[1]--;
            }
        } else if (stone == 2) {
            if (count[2] == count[0] && count[0] == 0) {
                return -1;
            }
            if (count[0] <= count[2] && count[0] > 0) {
                count[0]--;
            } else {
                stone += 2;
                stone = stone % 3;
                count[2]--;
            }
        }
        return stone;
    }

    public static void main(String[] args) {
        Solution2029 solution2029 = new Solution2029();
        System.err.println(solution2029.stoneGameIX(new int[] {19,2,17,20,7,17}));
    }
}
